<?php

class myfunction{
	
	// 表单输入防注入
	function test_input($data){
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
            }
	
	//由用户名查找用户信息
	function finduser($userid){
		$aa = new mysql;
        $aa->link("");

		$query= "select * from user_info where user_name = '".$userid."' ";
		$result= $aa->excu($query);
		$result= mysqli_fetch_array($result, MYSQLI_ASSOC);
		return $result;
		}
		
		
		
	//给密码加密
	function jiami($password){
		$password= hash('sha256',$password, false);
		return $password;
		}
	
	
	
	
	//用户上传头像
	function uploaduserpic($filename, $filetmp){
		
        $rand1=rand(0,9);
        $rand2=rand(0,9);
        $rand3=rand(0,9);
        $filenameadd=$rand1.$rand2.$rand3;

          if(!file_exists("img")){
               // 如果没有  目录，创建upload 目录权限默认为 777                
               mkdir("img"); 
	         //  echo "当前目录中，文件img不存在.已为您创建". "<br>";
              }
		
			// 将文件上传到 img 目录下
		$newsite= "D:/wamp64/www/04/user/img/" . $filenameadd.$filename;
		move_uploaded_file($filetmp, $newsite);
		return $newsite;
		}
		
		//管理员上传商品图片
	function uploadpropic($filename, $filetmp){
		
			// 将文件上传到 img 目录下
		$newsite= "upload/" .$filename;
		move_uploaded_file($filetmp, $newsite);
		return $newsite;
		}

        //生成验证码
    function randcode( $length ) {
	    // 密码字符集，可任意添加你需要的字符
	    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*-_~?';

	    $password = '';
	    for ( $i = 0; $i < $length; $i++ ) {
		    // 这里提供两种字符获取方式
		    // 第一种是使用 substr 截取$chars中的任意一位字符；
		    // 第二种是取字符数组 $chars 的任意元素
		    // $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
		    $password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
             }

	    return $password;
        }
   

    //检查用户是否登录
	function autologin(){
		if(empty($_SESSION['userid']) ){
			if(!empty($_COOKIE['userid']) && !empty($_COOKIE['password']) ){
				$userid= $_COOKIE['userid'];
				$password= $_COOKIE['password'];
				
				//var_dump($_COOKIE['userid']);
				
				$aa = new mysql;
                $aa->link("");
	         	$query= "select * from user_info where user_name = '".$userid."' and user_pw = '".$password."' ";
	        	$result= $aa->excu($query);
				
				if($result){
					$_SESSION['userid'] = $userid;
					//var_dump($_SESSION['userid']);
					return $_SESSION['userid'];
					}
				}
			
			}
		
		}
	
	
	  //购买商品信息插入数据库
	  function buyproinfo($proname, $proex01, $proex02, $num, $username,$time){
		  $aa = new mysql;
          $aa->link("");		

		  $query= "INSERT INTO prosold (proname, ex01, ex02, num, user_name, time, status) 
                       VALUES ('$proname', '$proex01', '$proex02', $num, '$username', '$time') ";
		  $result= $aa->excu($query);
		  
		  }
	
      //查找用户购买和购物车商品
	function findprocar($userid){
		$aa = new mysql;
        $aa->link("");


		$query= "select * from prosold where user_name = '".$userid."' and status = 1 ";
		$result= $aa->excu($query);
	//	$result= mysqli_fetch_array($result, MYSQLI_ASSOC);
		return $result;
		}


          //由商品id查找商品
	function findprobyid($proid){
		$aa = new mysql;
        $aa->link("");

		$query= "select * from product_info where id = '".$proid."' ";
		$result= $aa->excu($query);
		$result= mysqli_fetch_array($result, MYSQLI_ASSOC);
		return $result;
		}

          //查找所有管理人员
	function findallmanage(){
		$aa = new mysql;
        $aa->link("");

		$query= "select * from manage_info ";
		$result= $aa->excu($query);
		return $result;
		}


//由查找管理员用户信息
	function findmanageuser($userid){
		$aa = new mysql;
        $aa->link("");

		$query= "select * from manage_info where user_name = '".$userid."' ";
		$result= $aa->excu($query);
		$result= mysqli_fetch_array($result, MYSQLI_ASSOC);
		return $result;
		}


	
 }
?>




